package com.siyoumi.app.modules.app_book.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.siyoumi.app.entity.SysCorpPay;
import com.siyoumi.app.entity.SysSale;
import com.siyoumi.app.modules.user.entity.EnumCorpPayStatus;
import com.siyoumi.app.modules.user.service.SvcSysCorpPay;
import com.siyoumi.app.modules.user.service.SvcSysSale;
import com.siyoumi.app.modules.user.vo.VoCorpPayResetStatus;
import com.siyoumi.app.modules.user.vo.VoCorpPayUpdateStatus;
import com.siyoumi.app.modules.user.vo.VoSaleGetMoney;
import com.siyoumi.component.XApp;
import com.siyoumi.component.XBean;
import com.siyoumi.component.XEnumBase;
import com.siyoumi.component.http.InputData;
import com.siyoumi.controller.AdminApiController;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.util.XReturn;
import com.siyoumi.util.XStr;
import com.siyoumi.validator.XValidator;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/xadmin/app_book/app_book__sale__corp_pay__list")
public class app_book__sale__corp_pay__list
        extends AdminApiController {
    @GetMapping()
    public XReturn index() {
        setPageTitle("提现记录列表");

        InputData inputData = InputData.fromRequest();
        String type = inputData.input("type", "");

        String[] select = {
                "corppay_id",
                "corppay_create_date",
                "corppayq_order_id",
                "corppayq_order_id_ex",
                "corppay_true_name",
                "corppayq_bank_no",
                "corppayq_bank_code",
                "corppayq_fee",
                "corppayq_desc",
                "corppayq_status",
                "corppayq_status_date",
                "corppayq_return_str",
                "corppayq_api_status",
                "sale_uid",
                "sale_name",
        };
        JoinWrapperPlus<SysCorpPay> query = SvcSysCorpPay.getBean().listQuery(inputData);
        query.join(SysSale.table(), "sale_uid", "corppayq_uid");
        query.select(select);
        query.eq("sale_type", type);
        if (isStore()) {
            query.eq("sale_uid", getStoreId());
        }

        query.orderByDesc("corppay_id");

        IPage<SysCorpPay> page = new Page<>(inputData.getPageIndex(), inputData.getPageSize());
        //list
        IPage<Map<String, Object>> pageData = SvcSysCorpPay.getApp().getMaps(page, query);
        List<Map<String, Object>> list = pageData.getRecords();
        long count = pageData.getTotal();

        EnumCorpPayStatus enumStatus = XEnumBase.of(EnumCorpPayStatus.class);
        for (Map<String, Object> item : list) {
            SysCorpPay entity = XBean.fromMap(item, SysCorpPay.class);

            item.put("id", entity.getKey());
            item.put("true_name", XStr.markStar(entity.getCorppay_true_name(), 1, 1)); //真实姓名
            item.put("status", enumStatus.get(entity.getCorppayq_status())); //状态
            item.put("price", XApp.decimal100(BigDecimal.valueOf(entity.getCorppayq_fee()))); //提现金额
        }

        setPageInfo("status", enumStatus); //处理状态

        getR().setData("list", list);
        getR().setData("count", count);

        return getR();
    }

    /**
     * 重置提现状态
     * 更换订单号
     */
    @PostMapping("/reset_status")
    public XReturn resetStatus(@Validated VoCorpPayResetStatus vo, BindingResult result) {
        //统一验证
        XValidator.getResult(result);
        
        return SvcSysCorpPay.getBean().resetStatus(vo);
    }

    /**
     * 标记已提现
     */
    @PostMapping("/update_store_status")
    public XReturn updateStoreStatus(@Validated VoCorpPayUpdateStatus vo, BindingResult result) {
        //统一验证
        XValidator.getResult(result);

        vo.setCorppayq_status(1);
        return SvcSysCorpPay.getBean().updateStoreStatus(vo);
    }
}
